const keyEvent = function (onBounce, onRetract) {
  const originalHeight = document.documentElement.clientHeight || document.body.clientHeight;
  const resize = e => {
    onResize(e, onBounce, onRetract, originalHeight)
  }

  if (typeof onBounce === 'string') {
    window.removeEventListener('resize', resize)
    return false
  }

  window.addEventListener('resize', resize)
}

function onResize(e, onBounce, onRetract, originalHeight) {
  //键盘弹起与隐藏都会引起窗口的高度发生变化
  const resizeHeight = document.documentElement.clientHeight || document.body.clientHeight;

  if (resizeHeight * 1 < originalHeight * 1) {
    //当软键盘弹起，在此处操作
    if (typeof onBounce === 'function') {
      onBounce(e)
    }
  } else {
    //当软键盘收起，在此处操作
    if (typeof onRetract === 'function') {
      onRetract(e)
    }
  }
}

export default keyEvent